import { $http } from '@escook/request-miniprogram';
import { RespData } from '../entity/response/RespData';
const request = $http
const apihost = "http://127.0.0.1:8089";
request.beforeRequest = (res: { code: number; data: any; }) => {
    if(res.code === 500) {
        uni.showToast({
            icon: "error",
            duration: 2000,
            title: "服务器异常"
        })
    }else if(res.code != undefined){
        console.log(res)
        const respData = new RespData(res.data);
        if(respData.getCode() != 200) {
            uni.showToast({
                icon: "error",
                duration: 2000,
                title: respData.getMessage() || '错误'
            })
        }
    }
}

export function wxAppGet<T>(url :string,params: Record<string, any>): Promise<T> {
   return request.get(apihost + url,params);
}

export function wxAppPost<T>(url :string,params: Record<string, any>): Promise<T> {
    return request.post(apihost + url,params);
 }